package com.inossem.warehousestorage_inossem.activity.storage;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.utils.base.QRCodeUtils;
import com.inossem.utils.constant.QRCodeConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.adapter.storage.StorageManageMatrMoveAdapter;
import com.inossem.warehousestorage_inossem.bean.storage.StorageManageMatrBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.StorageManageUtils;

/**
 * Created by Administrator on 2017/7/16 0016.
 */

public class StorageManageMatrMoveActivity extends BaseActivity {
    private TextView title;

    private ListView StorageManageMaterielMoveListView;

    private StorageManageMatrMoveAdapter adapter;

    private StorageManageMatrBean.BodyBean materielItem;

    private String mId;

    private StorageManageMatrBean bean;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_storage_manage_matr_move);
        initView(this);
        initData(this);
    }

    @Override
    protected void onResumeImpl() {
        getData(StorageManageMatrMoveActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        StorageManageMaterielMoveListView = (ListView) activity.findViewById(R.id.list);
    }

    private void initData(Activity activity) {
        title.setText(R.string.activity_storage_manage_matr_move_position_information);
        ActivityUtils.initBackAndFunction(activity, R.string.activity_storage_manage_matr_move_back, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                String message = check(StorageManageMatrMoveActivity.this);
                if (message != null) {
                    DialogUtils.getConfirmDialog(StorageManageMatrMoveActivity.this, message, new DialogUtils.ConfirmDialogListener() {
                        @Override
                        public void onConfirm(Dialog dialog) {
                            StorageManageMatrMoveActivity.super.onBackPressed();
                        }
                    }).show();
                } else {
                    finish();
                }
            }
        });
        getExtra(activity);
        setmScanListener((String scanResult, int length) -> {
            String position = QRCodeUtils.DecryptQRCode(scanResult);
            EditText pos = (EditText) StorageManageMaterielMoveListView.findViewById(R.id.position);
            pos.setText(position);
        }, QRCodeConstant.TYPE_POSITION);
    }

    private void getExtra(Activity activity) {
        mId = activity.getIntent().getStringExtra(StorageManageExtra.ID);
    }

    private void getData(Context context) {
        materielItem = StorageManageUtils.getMaterielItemById(context, mId);
        addPositionListData(context, materielItem, StorageManageMaterielMoveListView);
    }

    private void addPositionListData(Context context, StorageManageMatrBean.BodyBean materielItem, ListView listView) {
        adapter = new StorageManageMatrMoveAdapter(context, materielItem, new StorageManageMatrMoveListener() {

            @Override
            public void onAddPosition(String materielId, String position, String number, String unit) {
                StorageManageUtils.addPosition(context, materielId, position, number, unit);
                getData(context);
            }

            @Override
            public void onDeletePosition(String materielId, String position) {
                StorageManageUtils.deletePosition(context, materielId, position);
                getData(context);
            }
        });

        listView.setAdapter(adapter);
    }

    private String check(Context context) {
        String message;
        message = StorageManageUtils.checkPositionList(context, materielItem);
        if (message != null) {
            return message;
        }
        return message;
    }

    @Override
    public void onBackPressed() {
        String message = check(StorageManageMatrMoveActivity.this);
        if (message != null) {
            DialogUtils.getConfirmDialog(StorageManageMatrMoveActivity.this, message, new DialogUtils.ConfirmDialogListener() {
                @Override
                public void onConfirm(Dialog dialog) {
                    StorageManageMatrMoveActivity.super.onBackPressed();
                }
            }).show();
        } else {
            StorageManageMatrMoveActivity.super.onBackPressed();
        }
    }

    public interface StorageManageMatrMoveListener {
        void onAddPosition(String materielId, String position, String number, String unit);

        void onDeletePosition(String materielId, String position);
    }

}
